

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JRadioButton;

/**
 * Custom button which tells the workspace when the user
 * has selected a chip to add
 * @author holde1ja
 */
public class ChipButton extends JRadioButton {
	private Chip chip;
	private Workspace workspace;
	
	/**
	 * Intializes the chip button
	 * @param imgPath The default icon that the chip takes
	 * @param pressedImgPath The icon for the chip when it is selected or pressed
	 * @param p_chip The chip that we will be creating copies of in the workspace
	 * @param p_workspace A handle to the workspace
	 */
	public ChipButton(String imgPath, String pressedImgPath, Chip p_chip, Workspace p_workspace)
	{
		this.setIcon(new ImageIcon(imgPath));
		this.setSelectedIcon(new ImageIcon(pressedImgPath));
		this.setPressedIcon(new ImageIcon(pressedImgPath));
		workspace = p_workspace;
		chip = p_chip;
		
		this.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() instanceof ChipButton)
				{
					((ChipButton)e.getSource()).processClickEvent();
				}
			}});
	}
	
	/**
	 * 
	 * @return the chip which we represent
	 */
	public Chip getChip()
	{
		return chip;
	}
	
	/**
	 * Tells the workspace to get ready to add our chip on its
	 * next click event.
	 */
	public void processClickEvent()
	{
		workspace.setChip(chip);
	}
}
